import { defineStore } from 'pinia';

/**
 * 应用全局状态管理
 * 包含应用配置、UI状态等
 */
export const useAppStore = defineStore('app', {
  state: () => ({
    // 加载状态
    loading: false,

    // 全局错误信息
    error: null as string | null,

    // 网络状态
    isOnline: true,

    // 应用配置
    config: {
      apiBaseUrl: '',
      version: '1.0.0',
      environment: 'development',
    },

    // UI状态
    ui: {
      sidebarCollapsed: false,
      theme: 'light',
      language: 'zh-CN',
    },

    // 设备信息
    device: {
      isMobile: false,
      isTablet: false,
      isDesktop: true,
      userAgent: '',
    },
  }),

  getters: {
    // 是否为移动设备
    isMobileDevice: state => state.device.isMobile,

    // 是否为暗色主题
    isDarkTheme: state => state.ui.theme === 'dark',

    // 获取当前语言
    currentLanguage: state => state.ui.language,

    // 是否显示加载状态
    isLoading: state => state.loading,
  },

  actions: {
    // 设置加载状态
    setLoading(loading: boolean) {
      this.loading = loading;
    },

    // 设置错误信息
    setError(error: string | null) {
      this.error = error;
    },

    // 清除错误
    clearError() {
      this.error = null;
    },

    // 设置网络状态
    setOnlineStatus(isOnline: boolean) {
      this.isOnline = isOnline;
    },

    // 更新应用配置
    updateConfig(config: Partial<typeof this.config>) {
      this.config = { ...this.config, ...config };
    },

    // 切换侧边栏状态
    toggleSidebar() {
      this.ui.sidebarCollapsed = !this.ui.sidebarCollapsed;
    },

    // 设置主题
    setTheme(theme: 'light' | 'dark') {
      this.ui.theme = theme;

      // 更新HTML类名
      if (process.client) {
        document.documentElement.classList.toggle('dark', theme === 'dark');
      }
    },

    // 设置语言
    setLanguage(language: string) {
      this.ui.language = language;
    },

    // 初始化设备信息
    initializeDevice() {
      if (process.client) {
        const userAgent = navigator.userAgent;
        this.device.userAgent = userAgent;

        // 简单的设备检测
        this.device.isMobile =
          /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
            userAgent
          );
        this.device.isTablet = /iPad|Android(?!.*Mobile)/i.test(userAgent);
        this.device.isDesktop = !this.device.isMobile && !this.device.isTablet;
      }
    },

    // 初始化网络状态监听
    initializeNetworkStatus() {
      if (process.client) {
        this.isOnline = navigator.onLine;

        window.addEventListener('online', () => {
          this.setOnlineStatus(true);
        });

        window.addEventListener('offline', () => {
          this.setOnlineStatus(false);
        });
      }
    },

    // 显示全局提示
    showMessage(
      message: string,
      type: 'success' | 'error' | 'warning' | 'info' = 'info'
    ) {
      // 这里可以集成到Toast组件
      console.log(`[${type.toUpperCase()}] ${message}`);

      // 如果是错误消息，设置到error状态
      if (type === 'error') {
        this.setError(message);
      }
    },

    // 初始化应用
    async initialize() {
      this.initializeDevice();
      this.initializeNetworkStatus();

      // 从环境变量获取配置
      this.updateConfig({
        environment: process.env.NODE_ENV || 'development',
        version: process.env.APP_VERSION || '1.0.0',
      });
    },
  },
});
